package cn.tlxqc.jsonwebtoken.controller;

import cn.tlxqc.jsonwebtoken.common.sign.Audience;
import cn.tlxqc.jsonwebtoken.common.utils.JwtHelper;
import cn.tlxqc.jsonwebtoken.common.vo.ResultVO;
import cn.tlxqc.jsonwebtoken.common.vo.ResultVOUtil;
import cn.tlxqc.jsonwebtoken.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class LoginController {

    @Autowired
    private Audience audience;

    @GetMapping("/login")
    public ResultVO login(@RequestParam("tel") String tel, @RequestParam("password") String password,
                          HttpServletRequest request){
        if ( !"18716546802".equals(tel) || !"123456".equals(password)){
            return ResultVOUtil.error("400","用户名或密码错误");
        }
        User user = new User("111","张三","tel");
        String jwtToken = JwtHelper.createJWT(user.getId(),user.getName(),user.getTel(),
                audience.getClientId(),audience.getName(),audience.getExpiresSecond()*1000,
                audience.getBase64Secret());
        String result_str = "bearer;"+jwtToken;
        return ResultVOUtil.success(result_str);
    }
}
